package com.gai.shop.dao.impl;

import com.gai.shop.dao.OrdersDao;
import com.gai.shop.entity.Address;
import com.gai.shop.entity.Orders;
import com.gai.shop.entity.User;
import com.gai.shop.util.DataBaseUtil;
import com.gai.shop.util.StrUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OrdersDaoImpl implements OrdersDao {
    private final QueryRunner QR = new QueryRunner();

    /**
     * 新增订单数据
     * @param orders
     * @return
     * @throws Exception
     */
    @Override
    public Integer insertIntoOrders(Orders orders) throws Exception {
        return QR.update(DataBaseUtil.getConnection(),
                "INSERT INTO orders VALUES (?,?,?,?,?,?);",
                orders.getOid(),
                orders.getUid(),
                orders.getAid(),
                orders.getOcount(),
                orders.getOtime(),
                orders.getOstate());
    }

    /**
     * 使用uid获取用户名下所有的订单信息
     * @param uid
     * @return
     */
    @Override
    public List<Orders> selectOrdersAllByUid(Integer uid) throws Exception{
        String sql = "SELECT o.`oid`,o.`uid`,o.`ocount`,o.`otime`,o.`ostate`,a.`aid`,a.`aname`,a.`aphone`,a.`adetail`,a.`astate`" +
                "FROM orders o,address a WHERE a.`aid` = o.`aid` AND o.`uid` = ?;";
        List<Map<String, Object>> mapList = QR.query(DataBaseUtil.getConnection(),
                sql,
                new MapListHandler(),
                uid);
        if (mapList == null) {
            return null;
        }
        List<Orders> ordersList = new ArrayList<>();
        for (Map<String, Object> query : mapList) {
            //声明需要封装数据的实体类
            Orders orders = new Orders();
            Address address = new Address();
            //数据封装
            BeanUtils.populate(orders,query);
            BeanUtils.populate(address,query);
            orders.setAddr(address);
            ordersList.add(orders);
        }
        return ordersList;
    }

    /**
     * 使用oid查询Orders数据
     * @param oid
     * @return
     */
    @Override
    public Orders getOrdersByOid(String oid) throws Exception{
        String sql = "SELECT o.oid,o.uid,o.ocount,o.otime,o.ostate,a.aid,a.aname,a.aphone,a.adetail,a.astate " +
                "FROM orders o,address a WHERE a.aid = o.aid AND o.oid = ?;";
        Map<String, Object> query = QR.query(DataBaseUtil.getConnection(),
                sql,
                new MapHandler(),
                oid);
        //声明需要封装的实体类
        Orders orders = new Orders();
        Address address = new Address();
        BeanUtils.populate(orders,query);
        BeanUtils.populate(address,query);
        orders.setAddr(address);
        return orders;
    }

    /**
     * 支付成功
     * @param oid
     * @return
     * @throws Exception
     */
    @Override
    public Integer updateOrderStateToPaySuccess(String oid) throws Exception {
        return QR.update(DataBaseUtil.getConnection(),
                "UPDATE orders SET ostate=1 WHERE oid = ?;",
                oid);
    }

    /**
     * 获取所有用户的订单数据 动态查询
     * @param uname
     * @param ostate
     * @return
     * @throws Exception
     */
    @Override
    public List<Orders> getAllOrders(String uname, String ostate) throws Exception {
        String sql = "SELECT o.oid,o.uid,o.aid,o.ocount,o.otime,o.ostate,u.uname " +
                "FROM orders o,`user` u WHERE o.uid = u.uid";
        List<Object> ol = new ArrayList<>();
        if (StrUtils.empty(uname)) {
            sql += " AND u.uname LIKE ?";
            ol.add("%" + uname + "%");
        }
        if (StrUtils.empty(ostate)){
            Integer state = Integer.valueOf(ostate);
            sql += " AND o.ostate = ?";
            ol.add(state);
        }
        List<Map<String, Object>> mapList = QR.query(DataBaseUtil.getConnection(),
                sql,
                new MapListHandler(),
                ol.toArray());
        if (mapList == null) {
            return null;
        }
        List<Orders> ordersList = new ArrayList<>();
        for (Map<String, Object> query : mapList) {
            //声明需要封装数据的实体类
            Orders orders = new Orders();
            User user = new User();
            BeanUtils.populate(orders,query);
            BeanUtils.populate(user,query);
            orders.setUser(user);
            ordersList.add(orders);
        }
        return ordersList;
    }

    /**
     *
     * @param oid
     * @param ostate
     * @return
     * @throws Exception
     */
    @Override
    public Integer updateOrderStateToSend(String oid, Integer ostate) throws Exception {
        return QR.update(DataBaseUtil.getConnection(),
                "UPDATE orders SET ostate = ? WHERE oid = ?;",
                ostate,
                oid);
    }
}
